<?php

namespace Common\Model;
use think\Model;

class UserMysqlDefaultCateModel extends BaseModel{

	//追加属性
    public $append = [
        'child_num',        //子分类条数
        'type_name',        //分类类型
        'child_list',//子分类列表
        'add_time_text',
        'edit_time_text',
        'stype_text'
	];

      /**
     * 分类类型
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStypeTextAttr($value, $data){
		$status_text = '--';
		if($data['stype']==0){
			$status_text = '标准数据库分类';
		}
		return $status_text;
    }
    
    //子分类数量
    function getChildListAttr($val, $data){
        $list =[];
        if($data['id']){
            $model = M('UserMysqlDefaultCate');
            $where=[];
            $where['pid']=$data['id'];
            $where['isdel']=0;
            $list = $model->where($where)->field('id,title,sht_c')->order('id asc')->select();
            foreach($list as $key=>$val){
                $where2['pid'] = $val['id'];
                $where2['isdel'] = 0;
                $list2 = $model->where($where2)->field('id,title,sht_c')->order('id asc')->select();
                if($list2){
                    $list[$key]['child_list'] = $list2;
                }
                
            }
        }
        return $data['child_list'] = $list;
    }

    //子分类数量
    function getChildNumAttr($val, &$data){
        $count = 0;
        $wdata['pid'] = $data['id'];
        $wdata['isdel'] = 0;
        $count = $this->where($wdata)->count();
        return $count;
    }

    //获取全部子分类
    function getChildIds($id){
        $ids = [];
        $model = M('UserMysqlDefaultCate');
        $wdata['pid'] = $id;
        $wdata['isdel'] = 0;
        $list = $model->where($wdata)->select();
        if($list){
            foreach($list as $val){
                $ids[] = $val['id'];
                $new_ids = $this->getChildIds($val['id']);
                $ids = array_merge($ids,$new_ids);
            }
        }
        return $ids;
    }



}